const {mysqlService} = require("../../js/service/mysqlService");

module.exports = {
    // http://localhost:9023/manage/example/getAll
    getAll: async (request, response) => {
        let sql = 'select id,name,price from mo_goods order by id desc limit 4';
        try {
            let res = await mysqlService(sql, []);
            // console.log(res);
            // res.send(res);
            return response.success(res);
            // res.error("自定义错误")
        } catch (error) {
            // console.error('Error executing SQL query:', error);
            // res.status(500).send('Internal Server Error');
            return response.error(error)
        }
    },

    // http://localhost:9023/manage/example/getDataById?id=70
    getDataById: async (request, response) => {
        let {id} = request.query;
        let sql = `select id, name, price
                   from mo_goods
                   where id = ?`;
        let res = await mysqlService(sql, [id]);
        if (res.length) {
            return response.success(res[0]);
        } else {
            return response.error("没有找到该商品")
        }
    },

    // http://localhost:9023/manage/example/add
    // post {"name":"商品名称","price":111}
    add: async (request, response) => {
        let {name, price} = request.body;
        let sql = `insert into mo_goods (name, price)
                   values (?, ?)`;
        try {
            let res = await mysqlService(sql, [name, price]);
            return response.success(res.insertId);
        } catch (error) {
            return response.error(error)
        }
    },

    // http://localhost:9023/manage/example/edit
    // post {"id":70,"name":"商品名称","price":111}
    edit: async (request, response) => {
        let {id, name, price} = request.body;
        let sql = `update mo_goods
                   set name  = ?,
                       price = ?
                   where id = ?`;
        try {
            let res = await mysqlService(sql, [name, price, id]);
            return response.success(res.affectedRows);
        } catch (error) {
            return response.error(error)
        }
    },

    // http://localhost:9023/manage/example/getPagination
    // post {"page":1,"size":10}
    getPagination: async (request, response) => {
        const page = parseInt(request.body.page) || 1;
        const size = parseInt(request.body.size) || 10;
        const offset = (page - 1) * size;
        const totalSql = "SELECT COUNT(*) AS total FROM mo_goods";
        const totalCountResult = await mysqlService(totalSql, []);
        const total = totalCountResult[0].total;
        let sql = `select id, name, price
                   from mo_goods
                   limit ?, ?`;
        let data = await mysqlService(sql, [offset, size]);
        return response.success(data, total);
    },

    // http://localhost:9023/manage/example/realDel?id=70
    realDel: async (request, response) => {
        let {id} = request.query;
        let sql = `delete
                   from mo_goods
                   where id = ?`;
        try {
            let res = await mysqlService(sql, [id]);
            if (res.affectedRows) {
                return response.success(res.affectedRows);
            } else {
                return response.error("没有找到该商品")
            }
        } catch (error) {
            return response.error(error)
        }
    },

}